package com.yidu.InventoryManagement.controller;

import com.yidu.InventoryManagement.entity.Fund;
import com.yidu.InventoryManagement.entity.SecurityStock;
import com.yidu.InventoryManagement.entity.Securlty;
import com.yidu.InventoryManagement.service.SecurityStockService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 梁俊
 * @date 2022/12/5
 * &@version 1.0
 */
@RestController
public class SecurityStockController {
    @Resource
    private SecurityStockService securityStockService;


    @RequestMapping("/selectSecurityStock")
    public Map selectSecurityStock(int page, int limit, String securityName,String date){
        System.out.println(date+"时间");
        //判断securityName是否为null
        if (securityName==null){
            securityName="";
        }
        //判断date是否为null
        if (date==null){
            date="";
        }
        //根据当前页，每页显示数，名字，时间查询加分页证券库存数据selectSecurityStock
        List<SecurityStock> securityStocks = securityStockService.selectSecurityStock(page, limit, '%'+securityName+'%','%'+date+'%');
        //根据名字时间查行数countSecurityStock
        int count = securityStockService.countSecurityStock('%'+securityName+'%','%'+date+'%');
        System.out.println("行数"+count);
        //map
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",securityStocks);
        return map;
    }


    //多个删除
    @RequestMapping("/deleteSecurityStock")
    public int deleteSecurityStock(String securityStockId){
        System.out.println("进来了删除的方法"+securityStockId);
        //根据id删除
        int i = securityStockService.deleteSecurityStock(securityStockId);
        if(i>0){
            System.out.println("删除成功"+i);
        }else{
            System.out.println("删除失败"+i);
        }
        return i;
    }


    //添加
    @RequestMapping("/insertSecurityStock")
    public int insertSecurityStock(HttpServletRequest request, SecurityStock securityStock,String date){
        //securityStock查询证券库存id
        String securityStockId = securityStock.getSecurityStockId();
        //查询证券库存idsecurityStockIdSelect
        List<SecurityStock> securityStocks = securityStockService.securityStockIdSelect();
        System.out.println(securityStocks+"pppppppppp");
        //最大idsecurityStockId1
        int  securityStockId1=0;
        //最新的最大id
        int i2=0;
        int i=0;
        //查询证券库存行数selectSecurityStockId
        int s = securityStockService.selectSecurityStockId();
        //查询到的行数判断==0
        if (s==0){
            //定义的id
            String idd="20221205";
            //证券库存对象
            SecurityStock securityStock1=new SecurityStock(idd,securityStock.getFundId(),date,securityStock.getSecurityId(),securityStock.getQuantity(),securityStock.getUnitCost(),securityStock.getMoney(),1,securityStock.getRemark());
            //添加语句insertSecurityStock
             i = securityStockService.insertSecurityStock(securityStock1);
        }else{
            //查询证券库存idfor循环
            for (SecurityStock stock : securityStocks) {
                System.out.println(stock.getSecurityStockId()+"llllll");
                //for取出来的最大id转成Integer
                securityStockId1 = Integer.parseInt(stock.getSecurityStockId());
                //得到的最大id大于i2
                if (securityStockId1>i2){
                    //i2==得到的最大id
                    i2=securityStockId1;
                }
            }
            //把i2转成String。v
            String id1 = String.valueOf(i2);
            //id自动增长类
            IdUtil idUtil=new IdUtil();
            //把转换String的id放到id自动增长类方法中，得到加一的id
            String id = idUtil.createID(id1);
            //证券库存对象
            SecurityStock securityStock1=new SecurityStock(id,securityStock.getFundId(),date,securityStock.getSecurityId(),securityStock.getQuantity(),securityStock.getUnitCost(),securityStock.getMoney(),1,securityStock.getRemark());
            //添加方法insertSecurityStock
            i = securityStockService.insertSecurityStock(securityStock1);
        }

        if(i>0){
            System.out.println("添加成功"+i);
        }else{
            System.out.println("添加失败"+i);
        }
        return i;
    }

    @RequestMapping("/funIdSelect")
    public HttpServletRequest funIdSelect(HttpServletRequest request){
        //查询基金id放到下拉框方法funIdSelect
        List<SecurityStock> securityStocks = securityStockService.funIdSelect();
        System.out.println(securityStocks+"pppp");
        //放到request中
        request.setAttribute("securityStocks",securityStocks);
        return request;
    }





    @RequestMapping("/updatesecurityStock")
    public int updatesecurityStock(SecurityStock securityStock){
        System.out.println("进来了修改"+securityStock.getDate());
        //根据证券库存查询基金id,证券id，初期标识方法selectOne
           SecurityStock securityStock2 = securityStockService.selectOne(securityStock.getSecurityStockId());
           System.out.println(securityStock2.getFundId()+"\t"+securityStock2.getSecurityId()+"\t"+securityStock2.getInitialState()+"哈哈哈哈");
           //证券库存对象
           SecurityStock   securityStock1=new SecurityStock(securityStock.getSecurityStockId(),securityStock2.getFundId(),securityStock.getDate(),securityStock2.getSecurityId(),securityStock.getQuantity(),securityStock.getUnitCost(),securityStock.getMoney(),securityStock2.getInitialState(),securityStock.getRemark());
           //修改方法updatesecurityStock
            int i = securityStockService.updatesecurityStock(securityStock1);
           if(i>0){
               System.out.println("修改成功"+i);
           }else{
               System.out.println("修改失败"+i);
           }
        return i;
    }




    @RequestMapping("/selectSecurityyyy")
    public Map selectSecurityyyy(int page, int limit, String securityName){
        System.out.println("进来了下拉表格");
        //判断securityName==null
        if (securityName==null){
            securityName="";
        }
        //根据当前页，每页显示数，名称查询加分页selectSecurityyyy
        List<Securlty> securlties = securityStockService.selectSecurityyyy(page, limit, '%'+securityName+'%');
        //根据名称查行数selectSecurityCount
        int count = securityStockService.selectSecurityCount('%' + securityName + '%');
        System.out.println("行数"+count);
        //map集合
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",securlties);
        return map;
    }



    @RequestMapping("/selectFundId")
    public Map selectFundId(int page, int limit){
        System.out.println("进来了下拉表格"+page+limit);
        //根据当前页，每页显示数查询加分页selectFundId
        List<Fund> funds = securityStockService.selectFundId(page, limit);
        //查行数selectFund
        int count = securityStockService.selectFund();
        System.out.println("行数"+count);
        //map
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",funds);
        return map;
    }

}

